package cn.lzd.mr.me;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by lzd on 2018/7/21.
 */
public class NameSplit {

    public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> {

        Text out = new Text();
        IntWritable outVal = new IntWritable(1);

        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String str = value.toString();
            try {
                String ci1 = str.substring(str.length() - 4, str.length());
                out.set(ci1);
                context.write(out, outVal);
                String ci2 = str.substring(str.length() - 6, str.length());
                out.set(ci2);
                context.write(out, outVal);
                String ci3 = str.substring(str.length() - 8, str.length());
                out.set(ci3);
                context.write(out, outVal);
            } catch (Exception e) {

            }
        }
    }

    public static class MyReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
        IntWritable outVal = new IntWritable();

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int count = 0;
            for (IntWritable value : values) {
                count += value.get();
            }
            outVal.set(count);
            context.write(key, outVal);
        }
    }

    public static void main1(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);
        job.setJarByClass(NameSplit.class);

        job.setMapperClass(NameSplit.MyMapper.class);
        job.setReducerClass(NameSplit.MyReduce.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job, new Path("/D:/1688/Namesplit/test3.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/D:/1688/Namesplit/out/"));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

    public static void main(String[] args) {

        try (FileOutputStream out = new FileOutputStream(new File("D:\\1688\\Namesplit\\out\\res"),true)){
            List<String> list = FileUtils.readLines(new File("D:\\1688\\Namesplit\\out\\part-r-00000"));

            list.stream().sorted((x, y) -> {
                Integer a = Integer.valueOf(x.split("\t")[1]);
                Integer b = Integer.valueOf(y.split("\t")[1]);
                return a.compareTo(b);
            }).forEach((x) -> {
                try {
                    out.write((x+"\r\n").getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
